

UserGroupTree = function(){
	UserGroupTree.superclass.constructor.call(this, {
		title : "用户组",
		rootVisible : false,
		lines : false,
		autoScroll : true,
		region : "west",
		width : 250,
		margins : "5 5 5 5",
		root : new Ext.tree.TreeNode("用户组")
    });

	this.on("added", this.onAdded, this);
	this._userGroups = [];
}

Ext.extend(UserGroupTree, Ext.tree.TreePanel, {
	onAdded : function(){
		var obj = this;
		var query = [];
		for (var i = 0; i < Authority.length; i ++)
			for (var j = 0; j < Authority[i].items.length; j ++)
				query.push(Authority[i].items[j].authority);
		Ext.Ajax.request({
			url: "/manage/getUserGroup",
			params : {auth : Ext.util.JSON.encode(query)},
			method : "GET",
			success: function(response, options){
				var data = Ext.util.JSON.decode(response.responseText);
				var treeNodes = [];
				for (var i = 0; i < data.length; i ++){
					if (data[i][3] == null) continue;
					for (var j = i + 1; j < data.length; j ++){
						if (data[j][0] == data[i][3]){
							var tmp = data[j];
							data[j] = data[i];
							data[i] = tmp;
						}
					}
				}
				for (var i = 0; i < data.length; i ++){
					var node = new Ext.tree.TreeNode(data[i][2]);
					treeNodes.push(node);
					if (data[i][3] == null) obj.root.appendChild(node);
					else {
						var loc;
						for (loc = 0; data[loc][0] != data[i][3]; loc ++);
						treeNodes[loc].appendChild(node);
					}
				}
				obj._userGroups = data;
			},
			failure : function(){
				Ext.Msg.alert("错误", "获取用户组错误...");
			}
		});
	}
});

Form_UserGroupManagement = function(){
	var obj = this;
	var userGroupTree = new UserGroupTree();
	
	var storeUserProxy = new Ext.data.HttpProxy({url : "/map/getSchoolMapLocPicsByLocId"});
	var storeUser = new Ext.data.Store({
		proxy : storeUserProxy,
		reader : new Ext.data.ArrayReader({
			fields : ["id", "name", "mobile", "email"]
		})
	});
	var smUser = new Ext.grid.CheckboxSelectionModel({
		listeners: {
			selectionchange: function(sm) {
				if (sm.getCount()) listUser.removeButton.enable();
				else listUser.removeButton.disable();
			}
		}
	});
	var listUser = new Ext.grid.GridPanel({
		store : storeUser,
		region : "center",
		margins : "5 5 5 0",
		columns : [
			{id : "id", header : "Id", dataIndex : "id", width : 50},
			{id : "name", header : "Name", dataIndex : "name", width : 100},
			{id : "mobile", header : "Mobile", dataIndex : "mobile", width : 80},
			{id : "email", header : "Email", dataIndex : "email", width : 100}
		],
		tbar : [{
			text : "在用户组删除此用户 ...",
			iconCls : "remove",
			disabled : true,
			listeners : {
				click : function(){
					Ext.Msg.show({
						title : "确认",
						msg : "确认删除此图片（PicCode：" + smSchoolMapLocPic.getSelected().data.picCode + "）？",
						buttons : Ext.MessageBox.YESNO,
						fn : function(button, text, opt){
							if (button == "yes"){
								Ext.Ajax.request({
									url: "/manage/removeSchoolMapLocPic",
									params : {id : smSchoolMapLocPic.getSelected().data.id},
									method : "GET",
									success: function(response, options){
										var data = Ext.util.JSON.decode(response.responseText);
										if (data == "success"){
											Ext.Msg.alert("提示", "删除成功！");
											storeSchoolMapLocPic.load();
										}  else {
											Ext.Msg.alert("错误", "错误：" + data + "。");
										}  
									},
									failure : function(){
										Ext.Msg.alert("错误", "无法连接到服务器。");
									}
								});
							}
						}
					});
				}
			}
		}],
		sm : smUser
	})
	
	Form_UserGroupManagement.superclass.constructor.call(this, {
		layout : "border",
		anchor : "100% 100%",
		items : [userGroupTree, listUser]
    });
}

Ext.extend(Form_UserGroupManagement, Ext.Panel, {
});

Ext.reg("App.UserGroupManagement", Form_UserGroupManagement);